package net.example.lesson1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhuhai
 */
public class FinalDemo {
    private final static Logger log = LoggerFactory.getLogger(FinalDemo.class);

    public void final1() {
        final String s1 = "s1";
        System.out.println(s1);
        String s2 = "2";
        System.out.println(s2);
        String s3 = s1 + "2";
        System.out.println(s3);
    }

    public void final2() {
        final String s1 = "1";
        String s2 = "2";
    }

    public void final3(final int num) {
        System.out.println(num);
    }

    public void final4() {
        //final 声明的list对象，表示引用地址不可变（不可以赋值）。但不影响list对象行为.如果不可以进行add方法，需要将List定义为final
        final List<String> list = new ArrayList<String>();
        list.add("A");
        list.add("B");
        list.add("C");
        System.out.println(list.toString());
//        final List<String> list1 = List.of("D","E","F","G");
//        System.out.println(list1);
        //下面代码就报错
//        list = list1;
    }

    public void final5() throws NoSuchFieldException,IllegalAccessException{
        final Info info = new Info();
        final Field field = info.getClass().getDeclaredField("info");
        field.setAccessible(true);
        field.set(info,9);
        System.out.println(field.get(info));
        System.out.println(info.info);
    }

    public static void main(String[] args) {
        final FinalDemo fd = new FinalDemo();
        try {
            fd.final5();
        } catch (final NoSuchFieldException e) {
            log.error(e+"");
        } catch (final IllegalAccessException e) {
            log.error(e+"");
        }
    }
}

class Info{
    final public int info = 1;
}
